package com.acce.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.acce.base.pojo.ArgsPojo;
import com.acce.base.pojo.ReturnPojo;
import com.acce.product.config.ErrorCodes;
import com.acce.product.config.HeaderType;
import com.acce.product.dao.IBuyGoodsDao;
import com.acce.product.dao.IPurchaseListDao;
import com.acce.product.exception.ApplicationException;
import com.acce.product.pojo.BuyGoods;
import com.acce.product.pojo.PurchaseHeader;
import com.acce.product.pojo.PurchaseList;
import com.acce.product.service.PurchaseListService;
import com.acce.product.utils.Utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service("purchaseListService")
public class PurchaseListServiceImpl implements PurchaseListService {

	private Logger logger = Logger.getLogger(PurchaseListServiceImpl.class);
	
	@Resource
	IPurchaseListDao purchaseListDao;
	
	@Resource
	IBuyGoodsDao buyGoodsDao;
	
	public ReturnPojo findPurchaseList(ArgsPojo args) {
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
//		String atUserId = jb.getString("atUserId");
		String frontId = jb.getString("frontId");
		
		ReturnPojo result = new ReturnPojo();
		result.setServiceCode(args.getServiceCode());
		
		PurchaseList purchaseList = new PurchaseList();
		purchaseList.setFrontId(frontId);
		purchaseList.setValidity("1");
		
		logger.info("nsy-product:findPurchaseHeader find purchaseLists");
		
		List<PurchaseList> purchaseLists = purchaseListDao.getPurchaseListByCondition(purchaseList);
		
		logger.info("nsy-product:findPurchaseHeader find purchaseLists: " + ((purchaseLists==null)?"null":purchaseLists.size()));
		result.setReturnCode(ErrorCodes.oper_success.getCode());
		result.setReturnMsg(ErrorCodes.oper_success.getMessage());
		result.setResult(purchaseLists);
		return result;
	}
	
	public ReturnPojo findPurchaseHeader(ArgsPojo args) {
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
//		String atUserId = jb.getString("atUserId");
		String sendCompanyId = jb.getString("sendCompanyId");
		String createTime = jb.getString("createTime");
		String direction = jb.getString("direction");
		String count = jb.getString("count");
		if(StringUtils.isEmpty(direction)) {
			direction = "0";
		}
		
		ReturnPojo result = new ReturnPojo();
		result.setServiceCode(args.getServiceCode());
		
		PurchaseHeader purchaseHeader = new PurchaseHeader();
		purchaseHeader.setSendCompanyId(sendCompanyId);
		purchaseHeader.setValidity("1");
		if(!StringUtils.isEmpty(createTime)) {
			logger.info("nsy-product:findPurchaseHeader find purchaseHeaders scroll up/down.");
			purchaseHeader.setCreateTime(createTime);
		} else {
			logger.info("nsy-product:findPurchaseHeader find purchaseHeaders first time query.");
		}
		purchaseHeader.setDirection(Integer.valueOf(direction));
		purchaseHeader.setCount(Integer.valueOf(count));
		//只查询未询价的申购单
		purchaseHeader.setPurchaseState("0");
		
		logger.info("nsy-product:findPurchaseHeader find purchaseHeaders");
		
		List<PurchaseHeader> purchaseHeaders = purchaseListDao.getPurchaseHeaderByCondition(purchaseHeader);
		
		logger.info("nsy-product:findPurchaseHeader find purchaseHeaders: " + ((purchaseHeaders==null)?"null":purchaseHeaders.size()));
		
		result.setReturnCode(ErrorCodes.oper_success.getCode());
		result.setReturnMsg(ErrorCodes.oper_success.getMessage());
		result.setResult(purchaseHeaders);
		return result;
	}

	@Transactional
	public ReturnPojo insertPurchaseList(ArgsPojo args) throws ApplicationException {
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
//		String atUserId = jb.getString("atUserId");
		JSONObject purchaseHeader = (JSONObject)jb.getJSONObject("purchaseHeader");
		JSONArray purchaseList = (JSONArray)jb.getJSONArray("purchaseList");

		String currTime = Utils.getCurrTimeMillions();
		
		ReturnPojo result = new ReturnPojo();
		result.setServiceCode(args.getServiceCode());
		
		PurchaseHeader purHeader = JSONObject.parseObject(purchaseHeader.toJSONString(), PurchaseHeader.class);
		String frontId = args.getEditUserId() + System.currentTimeMillis();
		purHeader.setHeaderType(HeaderType.purchase.getType());
		purHeader.setPurchaseId(Utils.makePurchaseId());
		purHeader.setFrontId(frontId);
		purHeader.setSendTime(currTime);
		purHeader.setCreateTime(currTime);
		purHeader.setValidity("1");
		purHeader.setSyncType("1");
		purHeader.setSyncTime(currTime);
		List<PurchaseList> list = new ArrayList<PurchaseList>();
		List<BuyGoods> goodsList = new ArrayList<BuyGoods>();
		for(int i=0; i<purchaseList.size(); i++) {
			JSONObject purList = (JSONObject)purchaseList.get(i);
			PurchaseList pList = JSONObject.parseObject(purList.toJSONString(), PurchaseList.class);
			pList.setPurchaseListId(Utils.makePurchaseListId());
			pList.setFrontId(frontId);
			pList.setValidity("1");
			list.add(pList);
			
			BuyGoods goods = new BuyGoods();
			goods.setGoodsKingdom(pList.getGoodsKingdom());
			goods.setSon(pList.getGoodsFamily());
			goods.setLevelNum("3");
			goods.setCompanyId(purHeader.getSendCompanyId());
			goods.setCompanyDefaultState("1");
			goods.setValidity("1");
			goods.setSyncType("1");
			goods.setSyncTime(currTime);
			goods.setProQuantity(pList.getPurchaseQuantity());
			logger.info("nsy-product::prepare to updateBatchBuyGoods: goodsKingdom-"+goods.getGoodsKingdom()
				+ ", son-" + goods.getSon() + ", companyId-" + goods.getCompanyId());
			goodsList.add(goods);
		}
		purchaseListDao.insertPurchaseHeader(purHeader);
		logger.info("nsy-product::insertPurchaseHeader success.");
		purchaseListDao.insertBatchPurchaseList(list);
		logger.info("nsy-product::insertBatchPurchaseList success.");
		
		buyGoodsDao.updateBatchBuyGoodsQuantity(goodsList);
		logger.info("nsy-product::updateBatchBuyGoods success.");
		
//		PushMessage pm = new PushMessage(purHeader);
//		PushMsgUtil.pushAndroidNotificationMessage(pm);
		
		result.setReturnCode(ErrorCodes.oper_success.getCode());
		result.setReturnMsg(ErrorCodes.oper_success.getMessage());
		return result;
	}

	public ReturnPojo updatePurchaseList(ArgsPojo args) throws ApplicationException {
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
//		String atUserId = jb.getString("atUserId");
		JSONObject purchaseHeader = (JSONObject)jb.getJSONObject("purchaseHeader");
		JSONArray purchaseList = (JSONArray)jb.getJSONArray("purchaseList");

		String currTime = Utils.getCurrTimeMillions();
		
		ReturnPojo result = new ReturnPojo();
		result.setServiceCode(args.getServiceCode());
		
		if(purchaseHeader != null) {
			logger.info("nsy-product::updatePurchaseList update purchase header.");
			PurchaseHeader purHeader = JSONObject.parseObject(purchaseHeader.toJSONString(), PurchaseHeader.class);
			purHeader.setSyncType("1");
			purHeader.setSyncTime(currTime);
			purchaseListDao.updatePurchaseHeader(purHeader);
		}
		
		if(purchaseList != null) {
			logger.info("nsy-product::updatePurchaseList update purchase list.");
			List<PurchaseList> list = new ArrayList<PurchaseList>();
			for(int i=0; i<purchaseList.size(); i++) {
				JSONObject purList = (JSONObject)purchaseList.get(i);
				PurchaseList pList = JSONObject.parseObject(purList.toJSONString(), PurchaseList.class);
				list.add(pList);
			}
			purchaseListDao.updateBatchPurchaseList(list);
		}
		
		result.setReturnCode(ErrorCodes.oper_success.getCode());
		result.setReturnMsg(ErrorCodes.oper_success.getMessage());
		return result;
	}
	
	public ReturnPojo deletePurchaseList(ArgsPojo args) throws ApplicationException {
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
//		String atUserId = jb.getString("atUserId");
		String frontId = jb.getString("frontId");

//		String currTime = Utils.getCurrTimeMillions();
		
		ReturnPojo result = new ReturnPojo();
		result.setServiceCode(args.getServiceCode());
		
		PurchaseHeader purchaseHeader = new PurchaseHeader();
		purchaseHeader.setFrontId(frontId);
		PurchaseList purchaseList = new PurchaseList();
		purchaseList.setFrontId(purchaseHeader.getFrontId());
		purchaseListDao.deletePurchaseHeader(purchaseHeader);
		purchaseListDao.deletePurchaseList(purchaseList);
		
		result.setReturnCode(ErrorCodes.oper_success.getCode());
		result.setReturnMsg(ErrorCodes.oper_success.getMessage());
		return result;
	}

}
